package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Dayens;
import com.xindaoapp.happypet.bean.Dayens.Dayen;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.CircleImageView;


/**
 * 粉丝列表
 * 
 * @author Yangqing
 * 
 */
public class DayenFansListActivity extends BaseActivity {

    private PullToRefreshListView vListView;
    /**
     * 判断加载粉丝还是达人
     */
    private String type;

    private boolean isFirst = true;

    private List<Dayen> dayen;
    private List<Dayen> fans;

    private View vHeader;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_dayenfans;
    }

    @Override
    protected void initViews() {
        vListView = (PullToRefreshListView) findViewById(R.id.listview);
        type = getIntent().getStringExtra(Constants.DAYEN_FANS_TYPE);

        vHeader = View.inflate(this, R.layout.dayenfans_top, null);
        RelativeLayout layout = (RelativeLayout) vHeader.findViewById(R.id.top_item);
        TextView tv = (TextView) vHeader.findViewById(R.id.textView);
        // tv.setTextSize(getResources().getDimension(R.dimen.default_text_size_small));
        if (type.equals(Constants.DAYEN_TYPE)) {
            setTopBarTitle("达人");
            layout.setBackgroundResource(R.drawable.dayen_top_item);
            tv.setText("单个话题的赞数超过500即可点亮达人勋章");
        } else if (type.equals(Constants.FANS_TYPE)) {
            setTopBarTitle("铁杆粉丝");
            layout.setBackgroundResource(R.drawable.fans_top_item);
            tv.setText("连续签到7天即可点亮铁杆粉丝勋章");
        }

        vListView.getRefreshableView().addHeaderView(vHeader);
        super.initViews();
    }


    @Override
    protected void initEvents() {

        vListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                if (type.equals(Constants.DAYEN_TYPE)) {
                    getDaYenData();
                } else if (type.equals(Constants.FANS_TYPE)) {
                    getFansData();
                }
            }
        });
        super.initEvents();
    }

    @Override
    protected void onLoadDatas() {
        if (type.equals(Constants.DAYEN_TYPE)) {
            getDaYenData();
        } else if (type.equals(Constants.FANS_TYPE)) {
            getFansData();
        }
        super.onLoadDatas();
    }

    private void getDaYenData() {
        if (CommonUtil.checkNetState(getApplicationContext())) {
            HappyPetApplication.get().getMoccaApi()
                    .getDaYenDatas("1", "10", "1", new IRequest<Dayens>() {
                        @Override
                        public void request(Dayens t) {
                            vListView.onRefreshComplete();
                            if (null == t && isFirst) {
                                onDataArrivedEmpty();
                            } else {
                                if (t.getResult().equals("0")) {
                                    onDataArrived(true);
                                    isFirst = false;
                                    if (null != t.array && t.array.size() > 0) {
                                        dayen = t.array;
                                        vListView.setAdapter(new DayenAdapter(
                                                DayenFansListActivity.this, dayen,
                                                Constants.PAGE_SIZE, R.layout.dayen_list_item,
                                                R.layout.item_loading));
                                    }
                                } else {
                                    if (isFirst) {
                                        onDataArrived(false);
                                    }
                                    if (!TextUtils.isEmpty(t.getMsg())) {
                                        Toast.makeText(DayenFansListActivity.this, t.getMsg(),
                                                Toast.LENGTH_SHORT).show();

                                    }
                                }
                            }
                        }
                    });
        } else {
            vListView.onRefreshComplete();
            onDataArrived(false);
        }
    }

    private void getFansData() {
        if (CommonUtil.checkNetState(getApplicationContext())) {
            HappyPetApplication.get().getMoccaApi()
                    .getDaYenDatas("1", "10", "2", new IRequest<Dayens>() {
                        @Override
                        public void request(Dayens t) {
                            vListView.onRefreshComplete();
                            if (null == t && isFirst) {
                                onDataArrived(false);
                            } else {
                                if (t.getResult().equals("0")) {
                                    onDataArrived(true);
                                    isFirst = false;
                                    if (null != t.array && t.array.size() > 0) {
                                        fans = t.array;
                                        fans = t.array;
                                        vListView.setAdapter(new FansAdapter(
                                                DayenFansListActivity.this, fans,
                                                Constants.PAGE_SIZE, R.layout.fans_item,
                                                R.layout.item_loading));
                                    }
                                } else {
                                    if (isFirst) {
                                        onDataArrived(false);
                                    }
                                    if (!TextUtils.isEmpty(t.getMsg())) {
                                        Toast.makeText(DayenFansListActivity.this, t.getMsg(),
                                                Toast.LENGTH_SHORT).show();

                                    }
                                }
                            }
                        }
                    });
        } else {
            vListView.onRefreshComplete();
            onDataArrived(false);
            Toast.makeText(getApplicationContext(), R.string.pLease_check_network,
                    Toast.LENGTH_SHORT).show();
        }

    }

    private class DayenAdapter extends XinDaoBaseAdapter<Dayen> {
        public DayenAdapter(Context context, List<Dayen> iniData, int pageSize, int res,
                int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent, final Dayen value) {
            DayenHolder holder = (DayenHolder) convertView.getTag();;
            if (holder == null) {
                holder = new DayenHolder();
                holder.userface = (CircleImageView) convertView.findViewById(R.id.userface);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.subject = (TextView) convertView.findViewById(R.id.subject);
                holder.gender = (ImageView) convertView.findViewById(R.id.gender);
                convertView.setTag(holder);
            }
            ImageLoader.getInstance().displayImage(value.userface, holder.userface);
            holder.name.setText(value.username);
            holder.subject.setText(value.subject);
            if (null != value.gender) {
                if (value.gender.equals("1")) {
                    holder.gender.setBackgroundResource(R.drawable.nanhai);
                } else if (value.gender.equals("2")) {
                    holder.gender.setBackgroundResource(R.drawable.nusheng);
                } else if (value.gender.equals("0")) {
                    holder.gender.setBackgroundResource(R.drawable.nusheng);
                }
            }
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =
                            new Intent(DayenFansListActivity.this, MyUserCenterActivity.class);
                    intent.putExtra(Constants.KEY_UID, value.uid);
                    intent.putExtra(Constants.KEY_USERNAME, value.username);
                    intent.putExtra(Constants.KEY_USERFACE, value.userface);
                    startActivity(intent);
                }
            });
            return convertView;
        }

        @Override
        public void nextPage(
                int start,
                int size,
                final com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<Dayen> iLoadNextPageData) {
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getDaYenDatas(String.valueOf(start), String.valueOf(size), "1",
                            new IRequest<Dayens>() {
                                @Override
                                public void request(Dayens t) {
                                    if (null != t && t.array != null) {
                                        iLoadNextPageData.loadNextPageData(t.array);
                                    }
                                }
                            });
        }
    }
    private class FansAdapter extends XinDaoBaseAdapter<Dayen> {
        public FansAdapter(Context context, List<Dayen> iniData, int pageSize, int res,
                int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent, final Dayen value) {
            FansHolder holder = (FansHolder) convertView.getTag();;
            if (holder == null) {
                holder = new FansHolder();
                holder.userface = (CircleImageView) convertView.findViewById(R.id.userface);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.subject = (TextView) convertView.findViewById(R.id.subject);
                holder.position = (TextView) convertView.findViewById(R.id.position);
                holder.gender = (ImageView) convertView.findViewById(R.id.gender);
                convertView.setTag(holder);
            }
            ImageLoader.getInstance().displayImage(value.userface, holder.userface);
            holder.name.setText(value.username);
            holder.position.setText("");
            holder.position.setBackgroundColor(getResources().getColor(R.color.transparent));
            if (value.position.equals("1")) {
                holder.position.setBackgroundResource(R.drawable.z_yellow);
            } else if (value.position.equals("2")) {
                holder.position.setBackgroundResource(R.drawable.z_red);
            } else if (value.position.equals("3")) {
                holder.position.setBackgroundResource(R.drawable.z_green);
            } else {
                holder.position.setText(value.position);
            }
            holder.subject.setText("连续签到" + value.mdays + "天");
            if (null != value.gender) {
                if (value.gender.equals("1")) {
                    holder.gender.setBackgroundResource(R.drawable.nanhai);
                } else if (value.gender.equals("2")) {
                    holder.gender.setBackgroundResource(R.drawable.nusheng);
                } else if (value.gender.equals("0")) {
                    holder.gender.setBackgroundResource(R.drawable.nusheng);
                }
            }
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =
                            new Intent(DayenFansListActivity.this, MyUserCenterActivity.class);
                    intent.putExtra(Constants.KEY_UID, value.uid);
                    intent.putExtra(Constants.KEY_USERNAME, value.username);
                    intent.putExtra(Constants.KEY_USERFACE, value.userface);
                    startActivity(intent);
                }
            });
            return convertView;
        }

        @Override
        public void nextPage(
                int start,
                int size,
                final com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<Dayen> iLoadNextPageData) {
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getDaYenDatas(String.valueOf(start), String.valueOf(size), "2",
                            new IRequest<Dayens>() {
                                @Override
                                public void request(Dayens t) {
                                    if (null != t && t.array != null) {
                                        iLoadNextPageData.loadNextPageData(t.array);
                                    }
                                }
                            });
        }
    }
    class DayenHolder {
        CircleImageView userface;
        TextView name;
        TextView subject;
        ImageView gender;
    }
    class FansHolder {
        CircleImageView userface;
        TextView name;
        TextView subject;
        TextView position;
        ImageView gender;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }


}
